package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.cait.bpmsuite.api.bpmmodle.BPMWorkItem;
import com.sugon.dao.SysOaProcessDao;
import com.sugon.entity.SysOaProcessEntity;
import com.sugon.modules.sparepart.model.vo.CommonApproveParamVO;
import com.sugon.modules.workflow.IWorkFlowManager;
import com.sugon.service.SysOaProcessService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 备件领料OA审核Service实现类
 *
 * @author sugon
 * @date 2021-12-15 10:54:05
 */
@Service("sysOaProcessService")
public class SysOaProcessServiceImpl implements SysOaProcessService {
    @Autowired
    private SysOaProcessDao sysOaProcessDao;
    @Autowired
    private IWorkFlowManager workFlowManager;

    @Override
    public List<SysOaProcessEntity> listByProcInstId(String procInstId) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("processInstId", procInstId);
        return queryList(param);
    }

    @Override
    public List<SysOaProcessEntity> list(String busType, Integer state) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("busType", busType);
        param.put("state", state);
        return queryList(param);
    }

    /**
     * @param id 主键
     * @return
     */
    @Override
    public SysOaProcessEntity queryObject(String id) {
        return sysOaProcessDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<SysOaProcessEntity> queryList(Map<String, Object> map) {
        return sysOaProcessDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysOaProcessDao.queryTotal(map);
    }

    /**
     * @param processWorkItemId 流程工作项ID
     * @return
     */
    @Override
    public SysOaProcessEntity queryByProcessWorkItemId(String processWorkItemId) {
        List<SysOaProcessEntity> sysOaProcessEntities = sysOaProcessDao.queryByWorkItemId(processWorkItemId);
        return CollectionUtil.isNotEmpty(sysOaProcessEntities) ? sysOaProcessEntities.get(0) : null;
    }

    /**
     * @param sysOaProcess 实体
     * @return
     */
    @Override
    public int save(SysOaProcessEntity sysOaProcess) {
        sysOaProcess.setId(RandomUtil.randomString(32));
        // 如果审批用户未指定时，默认用当前系统用户
        if (StrUtil.isEmpty(sysOaProcess.getApprover())) {
            sysOaProcess.setApprover(ShiroUtils.getUserId());
        }
        sysOaProcess.setApproveDate(DateUtil.date());
        return sysOaProcessDao.save(sysOaProcess);
    }

    @Override
    public int generateProcessHandleRecord(String busType, String busId, BPMWorkItem bpmWorkItem, Integer state, String remark) {
        SysOaProcessEntity processRecord = new SysOaProcessEntity();
        processRecord.setBusId(busId);
        processRecord.setBusType(busType);
        processRecord.setProcessInstId(bpmWorkItem.getProcInstId());
        processRecord.setProcessWorkitemId(bpmWorkItem.getId());
        processRecord.setWorkitemName(bpmWorkItem.getName());
        processRecord.setState(state);
        processRecord.setRemark(remark);
        return save(processRecord);
    }

    @Override
    public int generateProcessHandleRecord(String busType, String busId, String workitemName, Integer state, String remark) {
        SysOaProcessEntity processRecord = new SysOaProcessEntity();
        processRecord.setBusId(busId);
        processRecord.setBusType(busType);
        processRecord.setWorkitemName(workitemName);
        processRecord.setState(state);
        processRecord.setRemark(remark);
        return save(processRecord);
    }

    /**
     * @param approveParam 备件领料单审核参数VO
     * @return
     */
    @Override
    public int generateProcessHandleRecord(CommonApproveParamVO approveParam) {
        // 首先尝试从approveParam中获取，如果没有则根据workItemId查询工作项
        BPMWorkItem workItem = approveParam.getBpmWorkItem();
        if (workItem == null) {
            workItem = workFlowManager.getWorkItemById(approveParam.getWorkItemId());
            if (workItem == null) {
                return 0;
            }
        }
        SysOaProcessEntity processRecord = new SysOaProcessEntity();
        processRecord.setProcessInstId(workItem.getProcInstId());
        processRecord.setWorkitemName(workItem.getName());
        processRecord.setBusId(approveParam.getId());
        processRecord.setBusType("MATERIAL");
        processRecord.setProcessWorkitemId(approveParam.getWorkItemId());
        processRecord.setState(approveParam.getState());
        processRecord.setRemark(approveParam.getCheckRemark());
        processRecord.setApprover(approveParam.getApprover());
        return save(processRecord);
    }

    /**
     * @param sysOaProcess 实体
     * @return
     */
    @Override
    public int update(SysOaProcessEntity sysOaProcess) {
        return sysOaProcessDao.update(sysOaProcess);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return sysOaProcessDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return sysOaProcessDao.deleteBatch(ids);
    }
}
